// 1. 获取元素
var userInp = document.getElementById("user");
var Spans = document.getElementById("spans");

var pwdInp = document.getElementById("pwd");
// var pwdSpan = document.getElementById("pwd_span");
var isRem = document.getElementById("isRem");

btn.onclick = function () {
  console.log(111);
  // 判断输入是否为空
  if ((userInp.value =="" && pwdInp.value == "")) {
    
    Spans.innerHTML = "用户名、密码不能为空";
    Spans.style.color="red";
  }

  // 用户输入的用户名
  var account = userInp.value.trim();
  var pwd = pwdInp.value;

  if (account && pwd) {
    var xhr = new XMLHttpRequest();

    xhr.open("post", "../php/login_account.php", true);

    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.send(`account=${account}&pwd=${pwd}`);

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4 && xhr.status == 200) {
        var result = xhr.responseText;
        console.log(result);
        result = JSON.parse(result);
        console.log(result);

        var { status, msg, user } = result;

        if (status) {
          if (isRem.checked) {
            setCookie("lgc", user, 7);
          } else {
            setCookie("lgc", user);
          }
          var url=urlParse();
          var {returnUrl}=url;
          console.log(returnUrl);


          if(returnUrl){
              location.href=docodeURIComponent(returnUrl);
          }else{
              location.href="../html/index.html";
          }
        } else {
          //   alert(msg);
          Spans.textContent = msg;
          Spans.className = "err";
        }
      }
    };
  }
};
function urlParse() {
    var search = location.search;
    var data = {};
    if (search) {  //从注册页跳转过来  => 有数据
        var str = search.substring(1);
        console.log(str);
        var list = str.split("&");
        // console.log(list);
        list.forEach(function (item) {
            var key = item.split("=")[0];
            var val = item.split("=")[1];
            data[key] = val;
        })
    }
    return data;
}